/*************************************************************************
 * CS516 Software Engineering
 * Group Project: Android Finance Management System
 * Transaction.java
 * The transaction class is the used to store transaction user entered
 *************************************************************************/
package com.svu.se.group3;

import java.text.NumberFormat;
import java.util.ArrayList;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * (add your name below)
 * @author A2303 Lei Jiang
 * @author A2239 Tejaswi Davuluri
 * @author A2307 Lakshmi Kongara
 */
public class Transaction {
	/*
	 * attributes here
	 */
	private int id;
	private String mProductName;
	private float mPrice;
	private int mQuantity;
	private String mCategory;
	private String mPurchaseDate;
	private boolean isNew;

	/** 
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the mProductName
	 */
	public String getmProductName() {
		return mProductName;
	}

	/**
	 * @param mProductName the mProductName to set
	 */
	public void setmProductName(String mProductName) {
		this.mProductName = mProductName;
	}

	/**
	 * @return the mPrice
	 */
	public float getmPrice() {
		return mPrice;
	}

	/**
	 * @param mPrice the mPrice to set
	 */
	public void setmPrice(float mPrice) {
		this.mPrice = mPrice;
	}

	/**
	 * @return the mQuantity
	 */
	public int getmQuantity() {
		return mQuantity;
	}

	/**
	 * @param mQuantity the mQuantity to set
	 */
	public void setmQuantity(int mQuantity) {
		this.mQuantity = mQuantity;
	}

	/**
	 * @return the mCategory
	 */
	public String getmCategory() {
		return mCategory;
	}

	/**
	 * @param mCategory the mCategory to set
	 */
	public void setmCategory(String mCategory) {
		this.mCategory = mCategory;
	}

	/**
	 * @return the mPurchaseDate
	 */
	public String getmPurchaseDate() {
		return mPurchaseDate;
	}

	/**
	 * @param mPurchaseDate the mPurchaseDate to set
	 */
	public void setmPurchaseDate(String mPurchaseDate) {
		this.mPurchaseDate = mPurchaseDate;
	}

	/**
	 * @return the isNew
	 */
	public boolean isNew() {
		return isNew;
	}

	/**
	 * @param isNew the isNew to set
	 */
	public void setNew(boolean isNew) {
		this.isNew = isNew;
	}

	/*
	 * construction
	 */
	public Transaction(){
		this.id = 0;
		this.mProductName = "";
		this.mPrice = 0;
		this.mQuantity = 0;
		this.mCategory = "";
		this.mPurchaseDate = "";
		this.isNew = true;
	}
	
	/**
	 * save the object into database
	 * insert a new record if it is new
	 * otherwise, update
	 */
	public void save(){
		SQLiteDatabase db = Main.dbOpenHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DBOpenHelper.PRODUCT_NAME, this.mProductName);
		values.put(DBOpenHelper.CATEGORY, this.mCategory);
		values.put(DBOpenHelper.QUANTITY, this.mQuantity);
		values.put(DBOpenHelper.PRICE, this.mPrice);
		values.put(DBOpenHelper.PURCHASE_DATE, this.mPurchaseDate);
		
		// Check if the transaction is new
		if(this.isNew){
			// INSERT INTO
			db.insert(DBOpenHelper.TRANSACTION_TABLE_NAME, null, values);
			this.isNew = false;			
		}else{
			// UPDATE
			values.put(DBOpenHelper.ID, this.id);
			//db.update(DBOpenHelper.TRANSACTION_TABLE_NAME, values, whereClause, whereArgs);
		}
		db.close();
    }
	
	// Return total cost
	public String getTotal(){
		float total = this.mPrice * this.mQuantity;
		String formated = NumberFormat.getCurrencyInstance().format(total);
		return formated;
	}
	
	/*
	 * delete the current record from database
	 */
	public void delete(){
		SQLiteDatabase db = Main.dbOpenHelper.getWritableDatabase();
		
		db.close();
	}
	
	
	
	
	/**
	 * return a transaction by id
	 * @param id
	 * @return null or transaction object
	 */
	public static Transaction getbyId(int id){
		return null;
	}

	
	/**
	 * return transactions by date and category
	 * if not entered, use null
	 * @param date
	 * @param category
	 * @return null or list of transaction list
	 */
	public static ArrayList<Transaction> getbyDateCategory(String purchaseDate, String category){
		SQLiteDatabase db = Main.dbOpenHelper.getWritableDatabase();
		String where = null;
		ArrayList<Transaction> result = new ArrayList<Transaction>();
		
		// Add selection
		if(purchaseDate != null){
			
		}
		if(category != null){
			
		}
		
		// Query database
		Cursor cur = db.query(DBOpenHelper.TRANSACTION_TABLE_NAME, null, where, null, null, null, DBOpenHelper.PURCHASE_DATE+" DESC, "+DBOpenHelper.ID+" DESC");
		// Fetch each row into arraylist
		for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()){
			Transaction t = new Transaction();
			t.id = cur.getInt(0);
			t.mProductName = cur.getString(1);
			t.mCategory = cur.getString(2);
			t.mQuantity = cur.getInt(3);
			t.mPrice = cur.getFloat(4);
			t.mPurchaseDate = cur.getString(5);
			t.isNew = false;
			result.add(t);
		}
		
		db.close();
		return result;
	}
	
	/**
	 * excute SQL query
	 * @param query
	 */
	public void excuteQuery(String query){
	
	}

}
